import * as Blockly from 'blockly/core'

Blockly.Blocks['js_function_expression'] = {
  /**
   * Block for redering a function expression.
   * @this Blockly.Block
   */
  init: function () {
    this.setColour(290)
    this.appendDummyInput()
      .appendField('function')
    this.appendValueInput('NAME')
    this.appendValueInput('PARAM0')
      .appendField('(')
    this.appendDummyInput('END')
      .appendField(')')
    this.appendStatementInput('STACK')
    this.setInputsInline(true)

    this.setTooltip('Function expression.')

    this.setOutput(true)
  }
}

Blockly.JavaScript['js_function_expression'] = function (block) {
  var branch = Blockly.JavaScript.statementToCode(block, 'STACK')
  var name = Blockly.JavaScript.valueToCode(block, 'NAME', Blockly.JavaScript.ORDER_ATOMIC)
  var args = []
  for (var i = 0; i < block.paramCount; i++) {
    args[i] = Blockly.JavaScript.valueToCode(block, 'PARAM' + i, Blockly.JavaScript.ORDER_ATOMIC)
  }
  var code = 'yak.' + name + '=' + 'function ' + '(' + args.join(', ') + ') {\n' + branch + '}'
  if (block.outputConnection) {
    return [code, Blockly.JavaScript.ORDER_ATOMIC]
  } else {
    return code + ';\n'
  }
}

// 1--------------------------------内置的参数---------
// 代码块 输入代码
const params_input_json = {
  'type': 'params_input',
  'message0': '内置参数 %1',
  'args0': [
    {
      'type': 'field_input',
      'name': 'CODE',
      'text': ''
    }
  ],
  inputsInline: true,
  output: null,
  colour: '#808080',
  tooltip: '内置的参数',
  helpUrl: ''
}

// 创建积木
Blockly.Blocks['params_input'] = {
  init: function () {
    this.jsonInit(params_input_json)
  }
}

Blockly.JavaScript['params_input'] = function (block) {
  const code = block.getFieldValue('CODE')
  return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]
}

// 2------------------获取回调返回的参数---------
const getFuncParamsJson = {
  'type': 'get_func_params',
  'message0': '获取函数中第 %1 个参数',
  'args0': [
    {
      type: 'field_number',
      name: 'NUM',
      check: 'Number',
      min: 1,
      value: 1,
      max: 10,
      precision: 1
    }
  ],
  inputsInline: true,
  output: null,
  colour: 160,
  tooltip: '获取函数中指定次序的参数',
  helpUrl: ''
}

// 创建积木
Blockly.Blocks['get_func_params'] = {
  init: function () {
    this.jsonInit(getFuncParamsJson)
  }
}

Blockly.JavaScript['get_func_params'] = function (block) {
  const index = block.getFieldValue('NUM') - 1
  const code = `arguments[${index}]`
  return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]
}

// 3--------------------------------内置的参数 没有label---------
// 代码块 输入代码
const paramsInnerJson = {
  'type': 'params_builtin',
  'message0': '%1',
  'args0': [
    {
      'type': 'field_input',
      'name': 'CODE',
      'text': ''
    }
  ],
  inputsInline: true,
  output: null,
  colour: 160,
  tooltip: '内置的参数或变量',
  helpUrl: ''
}

// 创建积木
Blockly.Blocks['params_builtin'] = {
  init: function () {
    this.jsonInit(paramsInnerJson)
  }
}

Blockly.JavaScript['params_builtin'] = function (block) {
  const code = block.getFieldValue('CODE')
  return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]
}
